package com.ellfors.module_first.compose_test

import androidx.compose.runtime.mutableStateOf
import com.ellfors.common.base.BaseViewModel
import com.ellfors.common.http.HttpFactory
import com.ellfors.common.http.api.HttpApi
import com.ellfors.common.http.entity.LoginParam
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow

class TestComposeViewModel : BaseViewModel() {

    val successEvent = MutableSharedFlow<String>()
    val errorEvent = MutableStateFlow("")
    val startEvent = MutableSharedFlow<String>()
    val completeEvent = MutableSharedFlow<String>()

    val errorState = mutableStateOf("Null")

    fun doLogin(
        userName: String,
        password: String
    ) {
        requestHttp {
            HttpFactory
                .createApi(HttpApi::class.java)
                .login(LoginParam(userName, password))
        }.start {
            startEvent.emit("start")
            showLoading()
        }.success {
            successEvent.emit("success")
        }.error {
            errorState.value = it.errorMsg
            errorEvent.emit(it.errorMsg)
        }.complete {
            completeEvent.emit("complete")
            dismissLoading()
        }
    }
}